﻿--[[
    @Brief    登录升级处理
    @Author    jameszhai
    @Date    2017-05-17
--]]

EUpdateErrCode = 
{
    erNoUpdate    = 0;
    erNeedUpdate  = 1;
    erTVerTimeOut = 738197926;

};

EUpdateUI = 
{
    sPnlThis   = "pnlUpdate";

    -- 下载
    sPnlDown   = "pnlStep";
    sIcnStep   = "icnProg";                -- 更新动画
    sLblDnProg = "icnProg\\lblStep";    -- 更新动画
    sLvDnProg  = "pgUpdate";            -- 更新进度
    sLblState  = "lblStatus";            -- 状态

    -- 安装
    sPnlSetup  = "pnlVerInfo";
    sLblTitle  = "lblTitle";            -- 版本信息
    sEdtInProg = "edtTips";                -- 版本提示
    sLvInProg  = "pgUpdate";            -- 进度
    sLblState  = "lblStatus";            -- 状态提示
    sBtnDetail = "btnDetail";            -- 查看详细信息

    -- 错误提示
    sPnlError  = "pnlError";
    sLblIcon   = "lblIcon";                -- 错误提示ICON
    sLblTips   = "lblTips";                -- 错误提示
    sBnRestart = "btnRestart";            -- 重启TGP
    sBtnGetNew = "btnGetNew";            -- 获取新版本

    -- 更新图标
    sBeginTFC  = "ONEIMAGE:client_login\\upgrade\\begin\\begin.tfc;Center;0;";
    sUpdateTFC = "ONEIMAGE:client_login\\upgrade\\update\\update.tfc;Center;0;";
    sFinishTFC = "ONEIMAGE:client_login\\upgrade\\finish\\finish.tfc;Center;0;";
    sFeedBackTips = "richTxtFeedBack";

};

EUpdateConst = 
{
    nBtnSpace = 10;

    sWGWebUrl = LoadStr("WEGAME_COM_CLIENT_URL");    --"http://tgp.qq.com";

};

TLoginUpdate =
{
    -- 初始化界面
    InitView = function(nRoot)
        if TLoginUpdate.bFstInit then
            local pTPFUI = base_utility.get_component_adapter("ITpf_ui",
                                "ierd_tgp.tpf_ui.GetTPFUI();");
            if nil ~= pTPFUI then
                pTPFUI:AddLuaListener(ierd_tgp.liveupdate.kLiveupdateChecking, 
                                      "TLoginUpdate.OnChecking");
                pTPFUI:AddLuaListener(ierd_tgp.liveupdate.kLiveupdateUpdating, 
                                      "TLoginUpdate.OnUpdating");
                pTPFUI:AddLuaListener(ierd_tgp.liveupdate.kLiveupdateCheckFail,
                                      "TLoginUpdate.OnChkFail");
                pTPFUI:AddLuaListener(ierd_tgp.liveupdate.kLiveupdateUpdateFail,
                                      "TLoginUpdate.OnUpdateFail");
                pTPFUI:AddLuaListener(ierd_tgp.liveupdate.kLiveupdateNewversionReady,
                                      "TLoginUpdate.OnNewVerReady");
                pTPFUI:AddLuaListener(ierd_tgp.liveupdate.kLiveupdateNoUpdate,
                                      "TLoginUpdate.OnNoUpdate");
                pTPFUI:AddLuaListener(ierd_tgp.liveupdate.kLiveupdateRestarting,
                                      "TLoginUpdate.OnRestarting");
                pTPFUI:AddLuaListener(ierd_tgp.liveupdate.kLiveupdateCheckDone, 
                                      "TLoginUpdate.OnCheckDone");
                pTPFUI:AddLuaListener(ierd_tgp.liveupdate.kLiveupdateGotAnnouncement,
                                      "TLoginUpdate.OnGetVerTips");
                TLoginUpdate.bFstInit = false;
            else
                TGPLog.LogE("Update", "Init view, get TPF UI fail.");
            end;

            comm_center.listen_broadcast("i_broadcast_on_update_deps_status", 
                                         "TLoginUpdate.OnDepsUpdate");

            local     zRtn = comm_center.call_service("Svr_IsHideLogin", {});

            if 1 == zRtn["hide"] then
                TLoginUpdate.bHideLogin = true;
            else
                TLoginUpdate.bHideLogin = false;
            end;
        end;

        TLoginUpdate.dwRootID = nRoot;
        return true;
    end;

    -- 卸载界面
    UnInitView = function()
        TLoginUpdate.bInstall  = false;    -- 安装还是下载
        TLoginUpdate.sDetail   = nil;    -- Version detail URL
        TLoginUpdate.bFstInit  = true;    -- 首次初始化
        TLoginUpdate.bVisible  = false;

        -- 控件
        TLoginUpdate.dwRootID  = 0;
        TLoginUpdate.pPnlThis  = nil;    -- Self

        -- 下载
        TLoginUpdate.pPnlDown  = nil;    -- 下载页面
        TLoginUpdate.nLbDnProg = 0;        -- 下载进度提示
        TLoginUpdate.pLvDnProg = nil;    -- 下载进度
        TLoginUpdate.nLbDnStat = 0;        -- 下载状态
        
        -- 安装
        TLoginUpdate.pPnlSetup = nil;    -- 安装
        TLoginUpdate.pLblTitle = nil;    -- 版本提示
        TLoginUpdate.pEdtInProg= nil;    -- 版本信息
        TLoginUpdate.pLvInProg = nil;    -- 进度
        TLoginUpdate.nLbInStat = nil;    -- 状态提示
        TLoginUpdate.nBtnDetail= 0;        -- 查看详细信息

        -- 错误
        TLoginUpdate.pPnlError = nil;    -- 错误页面
        TLoginUpdate.pLblIcon  = nil;    -- 错误提示ICON
        TLoginUpdate.pLblTips  = nil;    -- 错误提示
        TLoginUpdate.pBnReload = nil;    -- 重启TGP
        TLoginUpdate.pBnGetNew = nil;    -- 获取新版本
    end;

    ShowView = function()
        if TLoginUpdate.CheckUI() then
            TLoginUpdate.pPnlThis:Show(TRUE);
            TLoginUpdate.bVisible = true;
        else
            TGPLog.LogE("Update", "Show update, view not created.");
        end;
    end;

    HideView = function()
        if (nil ~= TLoginUpdate.pPnlThis) then
            TLoginUpdate.pPnlThis:Show(FALSE);
            TLoginUpdate.bVisible = false;
        else
            TGPLog.LogE("Update", "Hide update, view not created.");
        end;
    end;

    IsVisible = function()
        return TLoginUpdate.bVisible;
    end;

    OnChecking = function(nEvtID, sJson)
        TGPLog.LogI("Update", "Checking update.");
    end;

    -- 检查失败
    OnChkFail = function(nEvtID, sJson)
           TGPLog.LogI("Update", "On live check fail.");
           TLoginUpdate.DepsUpdateFailed = false;

           local     zInfo = base_utility.json_to_table(sJson);

           if nil == zInfo then
               TGPLog.LogE("Update", "Live check fail, parameter error.");
               return;
           end;

           local     zParam = {};

           zParam["AppID"  ] = zInfo.app_id;
           zParam["ErrMsg" ] = zInfo.error_msg;
           zParam["ErrCode"] = zInfo.errcode;

           comm_center.send_broadcast("Msg_UpdateCheck", zParam);

           -- Show check error
           TLoginUpdate.ProcFailEvent(zInfo);
    end;

    OnUpdating = function(nEvtID, sJson)
        TGPLog.LogI("Update", "on_live_update_updating");

        -- 是否隐藏登录框模式
        if TLoginUpdate.bHideLogin then
            comm_center.call_service("Svr_ExitWeGame", {});
            TGPLog.LogI("Update", "Hide login mode, exit wegame.");
            return;
        end;

           local     zInfo = base_utility.json_to_table(sJson);

           if nil == zInfo then
               TGPLog.LogE("Update", "Live check fail, parameter error.");
               return;
           end;

           if (false == TLoginUpdate.bVisible) then
               local     zParam = {};

               zParam["AppID"  ] = zInfo.app_id;
               zParam["ErrMsg" ] = zInfo.error_msg;
               zParam["ErrCode"] = zInfo.errcode;
               comm_center.send_broadcast("Msg_OnDoUpdate", zParam);
           end;

           TLoginUpdate.SetProgress(zInfo["percent"]);
    end;

    OnUpdateFail = function(nEvtID, sJson)
        TGPLog.LogI("update", "on_live_update_updatefail");
        TLoginUpdate.DepsUpdateFailed = false;
           local     zInfo = base_utility.json_to_table(sJson);

           if nil == zInfo then
               TGPLog.LogE("Update", "Live check fail, parameter error.");
               return;
           end;

           local     zParam = {};

           zParam["AppID"  ] = zInfo.app_id;
           zParam["ErrMsg" ] = zInfo.error_msg;
           zParam["ErrCode"] = zInfo.errcode;

           comm_center.send_broadcast("Msg_UpdateCheck", zParam);
        comm_center.send_broadcast("Msg_UpdateFailed", zParam);
        TLoginUpdate.ProcFailEvent(zInfo);
    end;

    -- Process update fail event
    ProcFailEvent = function(zInfo)
           if TLoginUpdate.IsTextEmpty(zInfo.error_msg) then
               TLoginUpdate.ShowError(TStrHelper.GetString(EStrDefine.STR_VERCHECK_ERROR), true);
           else
               TLoginUpdate.ShowError(zInfo.error_msg, true);
           end;
    end;

    OnNewVerReady = function(nEvtID, sJson)
        local tbl_param = base_utility.json_to_table(sJson);
        local     zParam = {};
        if tbl_param ~= nil then
            zParam["AppID"  ] = tbl_param.app_id;
        end
        zParam["ErrCode"] = EUpdateErrCode.erNeedUpdate;

        comm_center.send_broadcast("Msg_UpdateCheck", zParam);

        TGPLog.LogI("Update", "New version ready.");
        TLoginUpdate.bInstall = true;
        TLoginUpdate.SetIsInstall(true);
    end;

    OnNoUpdate = function(nEvtID, sJson)
        TGPLog.LogI("Update", "on_live_update_noupdate");

           local     zInfo = base_utility.json_to_table(sJson);

           if nil == zInfo then
               TGPLog.LogE("Update", "Live check fail, parameter error.");
               return;
           end;

           local     zParam = {};

           zParam["AppID"  ] = zInfo.app_id;
           zParam["ErrCode"] = EUpdateErrCode.erNoUpdate;

           comm_center.send_broadcast("Msg_UpdateCheck", zParam);
    end;

    OnRestarting = function(nEvtID, sJson)
        TGPLog.Log("Update", "WeGame is restarting.");
    end;

    OnCheckDone = function(nEvtID, sJson)
        TGPLog.LogI("Update", "Live upate check done.");
    end;

    -- 获取到版本提示信息
    OnGetVerTips = function(nEvtID, sJson)
        TGPLog.LogI("Update", "OnGetVerTips, json:" .. sJson);

        local zData = base_utility.json_to_table(sJson);

        if nil == zData       or nil == zData.detail_url or
           nil == zData.title or nil == zData.content or
           0   == zData.content.length then
            TGPLog.LogI("Update", "OnGetVerTips, json string invalid:" .. sJson);
            return;
        end;

           TLoginUpdate.SetVerTips(zData.title, zData.content, zData.detail_url);
    end;

    IsValidID = function(nCtrlID)
        if nil == nCtrlID or 0 == nCtrlID then
            return false;
        end
        local ptr = ui_utility.get_ctrl_ptr_by_id(nCtrlID);
        return nil ~= ptr;
    end;

    -- 检查界面元素
    CheckUI = function()
        if nil ~= TLoginUpdate.pPnlThis then
            return true;
        end;

        TLoginUpdate.pPnlThis = ui_utility.get_ctrl_ptr_by_path(TLoginUpdate.dwRootID, EUpdateUI.sPnlThis)

        if nil == TLoginUpdate.pPnlThis then
            return false;
        end;

        local     nRoot = TLoginUpdate.pPnlThis:GetControlID();

        TLoginUpdate.pPnlDown  = ui_utility.get_ctrl_ptr_by_path(nRoot, EUpdateUI.sPnlDown);    -- 下载页面
        TLoginUpdate.pPnlSetup = ui_utility.get_ctrl_ptr_by_path(nRoot, EUpdateUI.sPnlSetup);    -- 安装
        TLoginUpdate.pPnlError = ui_utility.get_ctrl_ptr_by_path(nRoot, EUpdateUI.sPnlError);    -- 错误页面

        if nil == TLoginUpdate.pPnlDown or nil == TLoginUpdate.pPnlSetup or
           nil == TLoginUpdate.pPnlError then
               TGPLog.LogE("Update", "Get pages fail.");
               return false;
        end;

        local     nTempID = TLoginUpdate.pPnlDown:GetControlID();

        -- 下载
        TLoginUpdate.nIcDnProg = ui_utility.get_ctrl_id_by_path(nTempID, EUpdateUI.sIcnStep);
        TLoginUpdate.nLbDnProg = ui_utility.get_ctrl_id_by_path(nTempID, EUpdateUI.sLblDnProg);        -- 更新ICON动画
        TLoginUpdate.pLvDnProg = ui_utility.get_ctrl_ptr_by_path(nTempID, EUpdateUI.sLvDnProg);        -- 更新进度
        TLoginUpdate.nLbDnStat = ui_utility.get_ctrl_id_by_path (nTempID, EUpdateUI.sLblState);        -- 状态

        if not TLoginUpdate.IsValidID(TLoginUpdate.nLbDnProg) or 
           not TLoginUpdate.IsValidID(TLoginUpdate.nIcDnProg) or
            nil == TLoginUpdate.pLvDnProg or
           not TLoginUpdate.IsValidID(TLoginUpdate.nLbDnStat) then
               TGPLog.LogE("Update", "Get download control fail.");
               return false;
        end;
        TLoginUpdate.pLvDnProg:SetRange(0, 100);

        nTempID = TLoginUpdate.pPnlSetup:GetControlID();

        -- 安装
        TLoginUpdate.pLblTitle  = ui_utility.get_ctrl_ptr_by_path(nTempID, EUpdateUI.sLblTitle);    -- 版本信息
        TLoginUpdate.pEdtInProg = ui_utility.get_ctrl_ptr_by_path(nTempID, EUpdateUI.sEdtInProg);    -- 版本提示
        TLoginUpdate.pLvInProg  = ui_utility.get_ctrl_ptr_by_path(nTempID, EUpdateUI.sLvInProg);    -- 进度
        TLoginUpdate.nLbInStat  = ui_utility.get_ctrl_id_by_path (nTempID, EUpdateUI.sLblState);    -- 状态提示

        if nil == TLoginUpdate.pLblTitle or nil == TLoginUpdate.pEdtInProg or
           nil == TLoginUpdate.pLvInProg or 
           not TLoginUpdate.IsValidID(TLoginUpdate.nLbInStat) then
            TGPLog.LogE("Update", "Get install control fail.");
               return false;
        end;
        TLoginUpdate.pLvInProg:SetRange(0, 100);

        -- 查看详细信息
        TLoginUpdate.nBtnDetail = ui_utility.get_ctrl_id_by_path(nTempID, EUpdateUI.sBtnDetail);

        if (TLoginUpdate.IsValidID(TLoginUpdate.nBtnDetail)) then
            ui_utility.show_ctrl(TLoginUpdate.nBtnDetail, false);
            ui_utility.setproperty_by_id(TLoginUpdate.nBtnDetail, "OnClick", "TLoginUpdate.OnClkDetail");
        else
            TGPLog.LogE("Update", "Get view detail control fail.");
            return false;
        end;

        -- 错误提示
        nTempID = TLoginUpdate.pPnlError:GetControlID();

        TLoginUpdate.pLblIcon  = ui_utility.get_ctrl_ptr_by_path(nTempID, EUpdateUI.sLblIcon);        -- 错误提示ICON
        TLoginUpdate.pLblTips  = ui_utility.get_ctrl_ptr_by_path(nTempID, EUpdateUI.sLblTips);        -- 错误提示
        TLoginUpdate.pBnReload = ui_utility.get_ctrl_ptr_by_path(nTempID, EUpdateUI.sBnRestart);    -- 重启TGP
        TLoginUpdate.pBnGetNew = ui_utility.get_ctrl_ptr_by_path(nTempID, EUpdateUI.sBtnGetNew);    -- 获取新版本

        if nil == TLoginUpdate.pLblIcon  or nil == TLoginUpdate.pLblTips or 
           nil == TLoginUpdate.pBnReload or nil == TLoginUpdate.pBnGetNew then
               TGPLog.LogE("Update", "Get error page control fail.");
               return false;
        end;

        ui_utility.setproperty_by_id(TLoginUpdate.pBnReload:GetControlID(),
                                     "OnClick", "TLoginUpdate.OnClkRestart");
        ui_utility.setproperty_by_id(TLoginUpdate.pBnGetNew:GetControlID(),
                                     "OnClick", "TLoginUpdate.OnClkGetNew");

        -- 设置为安装
        TLoginUpdate.bInstall = false;
        TLoginUpdate.SetIsInstall(TLoginUpdate.bInstall);

        --TLoginUpdate.ShowError("更新失败，请尝试重启客户端或者下载新版本", true);

        TLoginUpdate.pFeedBackTips = ui_utility.get_ctrl_ptr_by_path(nTempID, 
            EUpdateUI.sFeedBackTips);
        if nil == TLoginUpdate.pFeedBackTips then
            TGPLog.LogE("Update", "feedback tips rich text is null.");
        else
            ui_utility.setproperty_by_id(TLoginUpdate.pFeedBackTips:GetControlID(), 
                "OnRTBClickHyperLink", "TLoginUpdate.OnClkFeedBack");
        end;
        return true;
    end;

    -- 卸载界面
    UnInitView = function()
        -- 下载页面
        TLoginUpdate.pPnlDown  = nil;    -- 下载页面
        TLoginUpdate.pLbDnProg = nil;    -- 下载进度提示
        TLoginUpdate.pLvDnProg = nil;    -- 下载进度
        TLoginUpdate.nLbDnStat = 0;        -- 下载状态
        
        -- 安装页面
        TLoginUpdate.pPnlSetup = nil;    -- 安装
        TLoginUpdate.pLblTitle = nil;    -- 版本提示
        TLoginUpdate.pEdtInProg= nil;    -- 版本信息
        TLoginUpdate.pLvInProg = nil;    -- 进度
        TLoginUpdate.nLbInStat = 0;        -- 状态提示
        TLoginUpdate.nBtnDetail= 0;

        -- 错误页面
        TLoginUpdate.pPnlError = nil;    -- 错误页面
        TLoginUpdate.pLblIcon  = nil;    -- 错误提示ICON
        TLoginUpdate.pLblTips  = nil;    -- 错误提示
        TLoginUpdate.pBnReload = nil;    -- 重启TGP
        TLoginUpdate.pBnGetNew = nil;    -- 获取新版本
    end;

    -- 设置是否为安装界面
    SetIsInstall = function(bInstall)
        TLoginUpdate.bInstall = bInstall;

        if nil ~= TLoginUpdate.pPnlSetup and 
           nil ~= TLoginUpdate.pPnlDown  and 
           nil ~= TLoginUpdate.pPnlError then
            if bInstall then
                TLoginUpdate.pPnlSetup:Show(TRUE);
                TLoginUpdate.pPnlDown:Show(FALSE);
            else
                TLoginUpdate.pPnlSetup:Show(FALSE);
                TLoginUpdate.pPnlDown:Show(TRUE);
            end;
            TLoginUpdate.pPnlError:Show(FALSE);
        else
            TGPLog.LogE("Update", "Set is install:"..tostring(bInstall));
        end;
    end;

    -- 设置进度
    SetProgress = function(nProg)
        if TLoginUpdate.nCurStep > nProg then
            TGPLog.LogI("Update", "Old progress bigger than current.");
            return;
        end;

        TLoginUpdate.nCurStep = nProg;

        if (TLoginUpdate.bInstall) then
            -- 安装进度
            if nil ~= TLoginUpdate.pLvInProg then
                TLoginUpdate.pPgUpdate:SetPos(nProg, TRUE);
            else
                TGPLog.LogE("Update", "Install progress control is nil.");
            end;

            if TLoginUpdate.IsValidID(TLoginUpdate.nLbInStat) then
                local     sProg = LoadStr("STR_LOGIN_UPGRADING")  .. tostring(nProg) .. "%";        -- "正在更新  " .. tostring(nProg) .. "%";
                ui_utility.setproperty_by_id(TLoginUpdate.nLbInStat, "Caption", sProg);
            else
                TGPLog.LogE("Update", "Install tips control is null.");
            end;
        else
            -- 更新状态
            if (TLoginUpdate.IsValidID(TLoginUpdate.nLbDnStat)) then
                local     sProg = LoadStr("STR_LOGIN_WG_UPGRADING") .. tostring(nProg) .. "%";    -- "客户端正在升级 " .. tostring(nProg) .. "%";
                ui_utility.setproperty_by_id(TLoginUpdate.nLbDnStat, "Caption", sProg);
            else
                TGPLog.LogE("Update", "Update lable is null.");
            end;

            -- 更新进度
            if nil ~= TLoginUpdate.pLvDnProg then
                TLoginUpdate.pLvDnProg:SetPos(nProg, TRUE);
            else
                TGPLog.LogE("Update", "Update progress is null.");
            end;
        end;
    end;

    -- 动画停止
    OnBeginAniEnd = function()
        TLoginUpdate.ChangeUpdateAni(1);
    end;

    OnFinishAniEnd = function()
        TLoginUpdate.ChangeUpdateAni(3);
    end;

    -- 设置状态
    SetStatus = function(sStatus)
        if (TLoginUpdate.bInstall) then
            if TLoginUpdate.IsValidID(TLoginUpdate.nLbInStat) then
                ui_utility.setproperty_by_id(TLoginUpdate.nLbInStat, 
                                             "Caption", sStatus);
            else
                TGPLog.LogE("Update", "Install status, status control is null.");
            end;
        else
            if TLoginUpdate.IsValidID(TLoginUpdate.nLbDnStat) then
                ui_utility.setproperty_by_id(TLoginUpdate.nLbDnStat, 
                                             "Caption", sStatus);
            else
                TGPLog.LogE("Update", "Install status, status control is null.");
            end;
        end;
    end;

    -- 设置版本提示
    SetVerTips = function(sTitle, sTips, sDetail)
        -- 提示标题栏
        if nil ~= TLoginUpdate.pLblTitle then
            if not TLoginUpdate.IsTextEmpty(sTitle) then
                ui_utility.setproperty_by_id(TLoginUpdate.pLblTitle:GetControlID(),
                                             "Caption", sTitle);
            else
                TGPLog.LogE("Update", "Title is empty.");
            end;
        else
            TGPLog.LogE("Update", "Title control is null.");
        end;

        -- 版本更新提示
        if nil ~= TLoginUpdate.pEdtTips then
            if not TLoginUpdate.IsTextEmpty(sTips) then
                ui_utility.setproperty_by_id(TLoginUpdate.pEdtTips:GetControlID(),
                                             "Caption", sTips);
            else
                TGPLog.LogE("Update", "Tips is empty.");
            end;
        else
            TGPLog.LogE("Update", "Update tips control is null.");
        end;

        -- 版本详细提示信息URL
        TLoginUpdate.sDetail = nil;

        if nil == sDetail or "" == sDetail then
            TGPLog.LogI("Update", "Detail url is null.");
            return;
        end;

        local sFind = string.match(sDetail, "%w+");

        if nil ~= sFind then
            TLoginUpdate.sDetail = sDetail;
            ui_utility.show_ctrl(TLoginUpdate.nBtnDetail, true);
        else
            TLoginUpdate.LogI("Update", "Detail url is empty.");
        end
      end;

    -- 显示错误界面
    ShowError = function(sError, bReload)
        if not TLoginUpdate.CheckUI() then
            TGPLog.LogI("Update", "Show error, check ui fail.");
            return;
        end;

        if nil ~= TLoginUpdate.pPnlSetup then
            TLoginUpdate.pPnlSetup:Show(FALSE);
        else
            TGPLog.LogI("Update", "Show error, check control is null.");
        end;

        if nil ~= TLoginUpdate.pPnlDown then
            TLoginUpdate.pPnlDown:Show(FALSE);
        else
            TGPLog.LogI("Update", "Show error, download control is null.");
        end;

        -- 错误提示
        if nil ~= TLoginUpdate.pLblTips then
            ui_utility.setproperty_by_id(TLoginUpdate.pLblTips:GetControlID(),
                                         "Caption", sError);
        else
            TGPLog.LogE("Update", "Show error, error tips control is null.");
        end;

        if nil ~= TLoginUpdate.pPnlError then
            local     rtTemp = TLoginUpdate.pPnlError:GetRectEx(FALSE,TRUE);

            if bReload then
                TLoginUpdate.pBnReload:Show(TRUE);    -- 重启TGP
                TLoginUpdate.pBnGetNew:Show(TRUE);    -- 获取新版本

                local     rtFst = TLoginUpdate.pBnReload:GetRectEx(FALSE,TRUE);
                local     rtSnd = TLoginUpdate.pBnGetNew:GetRectEx(FALSE,TRUE);
                local     nFstW = rtFst.right - rtFst.left;
                local     nSndW = rtSnd.right - rtSnd.left;

                -- 移动重启TGP按钮
                rtFst.left  = (rtTemp.right - rtTemp.left - 
                               nFstW - nSndW - EUpdateConst.nBtnSpace) / 2;
                rtFst.right = rtFst.left + nFstW;
                TLoginUpdate.pBnReload:MoveEx(rtFst,TRUE);

                -- 移动下载最新按钮位置
                rtSnd.left  = rtFst.left + EUpdateConst.nBtnSpace;
                rtSnd.right = rtSnd.left + nSndW;
                TLoginUpdate.pBnGetNew:MoveEx(rtSnd,TRUE);

                if true == TLoginUpdate.DepsUpdateFailed then
                    ui_utility.setproperty_by_id(TLoginUpdate.pBnReload:GetControlID(),
                    "Caption", LoadStr("TPF_LOGIN_RESTART_CLIENT"));
                    ui_utility.setproperty_by_id(TLoginUpdate.pBnGetNew:GetControlID(),
                    "Caption", LoadStr("DEPS_DN_CONTINUE_LOGIN"));
                end

            else
                TLoginUpdate.pBnReload:Show(FALSE);    -- 重启TGP
                TLoginUpdate.pBnGetNew:Show(TRUE);    -- 获取新版本

                local     rtBtn  = TLoginUpdate.pBnGetNew:GetRectEx(FALSE,TRUE);
                local     nWidth = rtBtn.right - rtBtn.left;

                -- 移动下载最新按钮位置
                rtBtn.left  = ((rtTemp.right - rtTemp.left) - nWidth) / 2;
                rtBtn.right = rtBtn.left + nWidth;
                TLoginUpdate.pBnGetNew:MoveEx(rtBtn,TRUE);
            end;
        else
            TGPLog.LogE("Update", "Get error pannel fail.");
            return ;
        end;

        if nil ~= TLoginUpdate.pPnlError then
            TLoginUpdate.pPnlError:Show(TRUE);
        end;
        TLoginUpdate.ShowFeedBackTips(true);
    end;

    -- 重启TGP
    OnClkRestart = function(pUILib, nBtnID)
        comm_center.call_service("Svr_RestartWG", {});
    end;

    -- 获取新版本
    OnClkGetNew = function(pUILib, nBtnID)
        if true == TLoginUpdate.DepsUpdateFailed then
            TLoginCtrl.SwitchPage(ELoginPage.lpSecCheck);
        else
            web_utility.open_browser(EUpdateConst.sWGWebUrl, false, true);
        end
    end;

    -- 点击查看详细信息
    OnClkDetail = function(pUILib, nBtnID)
        if nil ~= TLoginUpdate.sDetail then
            -- 打开浏览器
            web_utility.open_browser(TLoginUpdate.sDetail, false, true);
            TGPLog.LogI("Update", "View detail:" .. TLoginUpdate.sDetail);
        else
            TGPLog.LogE("Update", "Click detail button, detail url is null.");
        end;
    end;

    -- 文本是否为空
    IsTextEmpty = function(sText)
        if nil == sText or "" == sText then
            return true;
        else
            return (nil ~= string.match(sText, "^%s*$"));
        end;
    end;

    -- 更改状态
    ChangeUpdateAni = function(nStep)
        --[[
        if 0 == nStep then
            ui_utility.setproperty_by_id(TLoginUpdate.nIcDnProg, 
                                         "Background", EUpdateUI.sBeginTFC);
        elseif 1 == nStep then
            ui_utility.setproperty_by_id(TLoginUpdate.nIcDnProg, 
                                         "Background", EUpdateUI.sUpdateTFC);
        elseif 2 == nStep then
            ui_utility.setproperty_by_id(TLoginUpdate.nIcDnProg, 
                                         "Background", EUpdateUI.sFinishTFC);
        else
            ui_utility.setproperty_by_id(TLoginUpdate.nIcDnProg, "Background", "NONE");            
        end;
        ]]
    end;

    OnDepsUpdate = function(param)
        TGPLog.LogI("Update", "download deps");
        if TLoginUpdate.bHideLogin then
            comm_center.call_service("Svr_ExitWeGame", {});
            TGPLog.LogI("Update", "Hide login mode, exit wegame.");
            return;
        end;

        if nil == param or type(param) ~= 'table' then
            TGPLog.LogE("Update", "invalid deps update param");
            return;
        end

        if false == TLoginUpdate.bVisible then
            local zParam = {};
            zParam["AppID"  ] = 0;
            zParam["ErrMsg" ] = "";
            zParam["ErrCode"] = 0;
            comm_center.send_broadcast("Msg_OnDoUpdate", zParam);
        end;
        TLoginUpdate.SetProgress(param["percent"]);

        if 0 ~= param.error_code then
            TGPLog.LogE("Update", "update deps error " .. tostring(param.error_code));
            TLoginUpdate.DepsUpdateFailed = true;
            local zInfo = {};
            zInfo["error_msg"] = 
                    LoadStr("DEPS_DOWNLOAD_FAILED") .. ", " .. LoadStr("TPF_ERROR_CODE") ..
                    tostring(param.error_code);
            TLoginUpdate.ProcFailEvent(zInfo);
        elseif true == param["finish"] then
            comm_center.call_service("Svr_RestartWG", {});
        end
    end;
    
     ShowFeedBackTips = function(bShow)
        local is_qq_login = 
            TLoginModule.GetLoginAccountType() == TLoginModule.LOGIN_ACCOUNT_TYPE.kQQLogin;
        if nil ~= TLoginUpdate.pFeedBackTips then
            if bShow and is_qq_login then
                local sTemp = U8toCUIStr(LoadStr("STR_LOGIN_FAILED_FEEDBACK"));
                TLoginUpdate.pFeedBackTips:Clear();
                TLoginUpdate.pFeedBackTips:PushBackHtmlString(sTemp);
                TLoginUpdate.pFeedBackTips:Show(TRUE);
            else
                TLoginUpdate.pFeedBackTips:Show(FALSE);
            end
        end;
    end;

     -- 去反馈
    OnClkFeedBack = function(pUILib, nBtnID)
        web_utility.open_new_browser(LoadStr("LOGIN_FAILED_FEEDBACK_URL"), false, true);
    end;
    -- 状态
    bHideLogin= false;    -- 是否隐藏登录框模式
    bInstall  = false;    -- 安装还是下载
    sDetail   = nil;    -- Version detail URL
    bFstInit  = true;    -- 首次初始化
    bVisible  = false;
    nCurStep  = 0;        -- Current progress

    -- 控件
    dwRootID  = 0;
    pPnlThis  = nil;    -- Self

    -- 下载
    pPnlDown  = nil;    -- 下载页面
    nLbDnProg = nil;    -- 下载进度提示
    pLvDnProg = nil;    -- 下载进度
    nLbDnStat = nil;    -- 下载状态
    
    -- 安装
    pPnlSetup = nil;    -- 安装
    pLblTitle = nil;    -- 版本提示
    pEdtInProg= nil;    -- 版本信息
    pLvInProg = nil;    -- 进度
    nLbInStat = nil;    -- 状态提示
    nBtnDetail= 0;        -- 查看详细信息

    -- 错误
    pPnlError = nil;    -- 错误页面
    pLblIcon  = nil;    -- 错误提示ICON
    pLblTips  = nil;    -- 错误提示
    pBnReload = nil;    -- 重启TGP
    pBnGetNew = nil;    -- 获取新版本

};